///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_MAT_IO_H_
#define _BUOLA_MAT_IO_H_

#include <buola/mat/cmat.h>

namespace buola { namespace mat {

template<typename tSerializer,typename tDerived>
inline void serialize(tSerializer &pS,const CExpr<tDerived> &pExpr)
{
    const auto &lExpr=detail::element_accessible<detail::ACCESS_ONCE>(pExpr.Derived());
    
    pS << lExpr.Derived().Rows() << lExpr.Derived().Cols();
    
    for(int i=0;i<lExpr.Rows();i++)
    {
        for(int j=0;j<lExpr.Cols();j++)
            pS << lExpr(i,j);
    }
}

template<typename tSerializer,typename tScalar,size_t vRows,size_t vCols>
inline void deserialize(tSerializer &pS,CMat<tScalar,vRows,vCols> &pMat)
{
    size_t lRows,lCols;
    pS >> lRows >> lCols;
    
    pMat.Resize(lRows,lCols);
    
    for(int i=0;i<lRows;i++)
    {
        for(int j=0;j<lCols;j++)
            pS >> pMat(i,j);
    }
}

/*namespace mat*/ } /*namespace buola*/ }

#endif
